package com.netcracker.ui.command;

import com.netcracker.entity.User;
import com.netcracker.entity.Right;
import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.UserService;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class ViewProfileCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ViewProfileCommand.class);
    private final ServiceFacade serviceFacade;

    public ViewProfileCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        Right right = (Right) request.getSession().getAttribute("right");
        if (right.equals(Right.USER) || right.equals(Right.ADMIN)) {
            Integer userID = (Integer) request.getSession().getAttribute("id");
            UserService userService = serviceFacade.getUserService();
            try {
                User user = userService.getUserInformation(userID);
                request.setAttribute("user", user);
                return "?action=allCountries&for=user";
               // return "UserProfile.jsp";
            } catch (DataTransmittingException ex) {
                request.setAttribute("error", "Ошибка обращения к БД.");
                LOGGER.error("DB error: ", ex);
                return "index.jsp";
            } catch (NullDataException ex) {
                request.setAttribute("error", "Пользователь не найден.");
                return "index.jsp";
            }
        } else {
            request.setAttribute("error", "Зарегистрируйтесь или войдите на сайт.");
            return "registration.jsp";
        }
        
    }  
}